
if (SERVER) then

	AddCSLuaFile( "shared.lua" )
	SWEP.Weight				= 5
	SWEP.AutoSwitchTo		= false
	SWEP.AutoSwitchFrom		= false

end

if ( CLIENT ) then

	SWEP.DrawAmmo			= true
	SWEP.DrawCrosshair		= false
	SWEP.ViewModelFOV		= 70
	SWEP.ViewModelFlip		= true
	SWEP.CSMuzzleFlashes	= true
	SWEP.BounceWeaponIcon	= false
	// This is the font that's used to draw the death icons
	surface.CreateFont( "csd", ScreenScale( 30 ), 500, true, true, "CSKillIcons" )
	surface.CreateFont( "csd", ScreenScale( 60 ), 500, true, true, "CSSelectIcons" )

end

SWEP.Author			= "Counter-Strike"
SWEP.Contact		= ""
SWEP.Purpose		= ""
SWEP.Instructions	= ""
SWEP.Category			= "Counter-Strike DS"

// Note: This is how it should have worked. The base weapon would set the category
// then all of the children would have inherited that.
// But a lot of SWEPS have based themselves on this base (probably not on purpose)
// So the category name is now defined in all of the child SWEPS.
//SWEP.Category			= "Counter-Strike"

SWEP.Spawnable			= false
SWEP.AdminSpawnable		= false
SWEP.AutoReloading		= false

SWEP.Primary.Sound			= Sound( "Weapon_AK47.Single" )
SWEP.Primary.Recoil			= 1.5
SWEP.Primary.Damage			= 40
SWEP.Primary.NumShots		= 1
SWEP.Primary.Cone			= 0.02
SWEP.Primary.Delay			= 0.15

SWEP.Primary.ClipSize		= -1
SWEP.Primary.DefaultClip	= -1
SWEP.Primary.Automatic		= false
SWEP.Primary.Ammo			= "none"

SWEP.Secondary.ClipSize		= -1
SWEP.Secondary.DefaultClip	= -1
SWEP.Secondary.Automatic	= false
SWEP.Secondary.Ammo			= "none"
SWEP.Secondary.Delay		= 2

function SWEP:Initialize()
	if SERVER then
		self:SetNPCMinBurst( 30 )
		self:SetNPCMaxBurst( 30 )
		self:SetNPCFireRate( 0.01 )
	end
	if self.Primary.Sound then util.PrecacheSound( self.Primary.Sound ) end
	if self.Secondary.Sound then util.PrecacheSound( self.Secondary.Sound ) end
	self:SetWeaponHoldType( self.HoldType )
end

function SWEP:Equip( NewOwner ) self.Owner = NewOwner return true end


function SWEP:Reload()
	--if self.Weapon:Clip1() >= self.Primary.ClipSize then return end
	if self.Owner:KeyDown( IN_ATTACK ) then
		self.NeedToReload = true
		return
	end
	self.NeedToReload = false
	
	self.Weapon:DefaultReload( ACT_VM_RELOAD )
end

function SWEP:Deploy()

	self.Weapon:SetNextPrimaryFire( CurTime() + 1 )

	self.Weapon:SendWeaponAnim( ACT_VM_DRAW )

	return true
end

function SWEP:Think()
	if SERVER and self.NeedToReload then
		self:Reload()
	end
	
	if self.Weapon:Clip1() < 1 and !self.Owner:KeyDown( IN_ATTACK ) and !self.AutoReloading then
		self:Reload()
		self.AutoReloading = true
		return
	end

	if self.Weapon:Clip1() > 0 then
		self.AutoReloading = false
	end
end

function SWEP:PrimaryAttack()

	self.Weapon:SetNextPrimaryFire( CurTime() + self.Primary.Delay )
	self.Weapon:SetNextSecondaryFire( CurTime() + self.Primary.Delay )
	
	if !self:CanPrimaryAttack() or self.Owner:WaterLevel() > 2 or self.Weapon:Clip1() < 1 then return end

	local LastShootTime = self.Weapon:GetNetworkedFloat( "LastShootTime", 0 )
	local Crouch = ( self.Owner:Crouching() and self.Owner:OnGround() ) and 2 or 1

	self:CSShootBullet( self.Primary.Damage, self.Primary.Recoil, self.Primary.NumShots, self.Primary.Cone / math.Clamp( ( CurTime() - LastShootTime ) * 1.3 , 0.001, 1 ) / Crouch )
	
	if self.Owner:IsNPC() then return end

	self.Owner:ViewPunch( Angle( math.Rand( -0.5, -0.1 ) * self.Primary.Recoil, math.Rand( -0.5, 0.5 ) *self.Primary.Recoil, 0 ) )

	if ( (SinglePlayer() && SERVER) || CLIENT ) then
		self.Weapon:SetNetworkedFloat( "LastShootTime", CurTime() )
	end
end

function SWEP:CSShootBullet( dmg, recoil, numbul, cone )

	numbul = numbul or 1
	cone = cone or 0.01
	cone = ( self.Owner:GetVelocity():Length() > 150 ) and cone + ( self.Owner:GetVelocity():Length() / 10000 ) or cone

	local bullet = {}
	bullet.Num 		= numbul
	bullet.Src 		= self.Owner:GetShootPos()			// Source
	bullet.Dir 		= self.Owner:GetAimVector()			// Dir of bullet
	bullet.Spread 	= Vector( cone, cone, 0 )			// Aim Cone
	bullet.Tracer	= 2									// Show a tracer on every x bullets 
	bullet.Force	= 5									// Amount of force to give to phys objects
	bullet.Damage	= dmg
	local SWEPOWNER = self.Owner
	bullet.Callback	= function( attacker, tr, dmginfo )
			local dist = ( tr.HitPos - tr.StartPos ):Length()
			if dist > 500 then return end
			local traceres = {}

			local shdist = 40
			shdist = ( tr.MatType == 67 ) and 15 or shdist
			shdist = ( tr.MatType == 77 ) and 5 or shdist

			traceres.start = tr.HitPos + tr.Normal * shdist
			traceres.endpos = tr.HitPos
			local trace = util.TraceLine( traceres )
			if trace.Hit then
				local bullet = {}
				bullet.Num 		= numbul
				bullet.Src 		= trace.HitPos - trace.Normal			// Source
				bullet.Dir 		= tr.Normal	// Dir of bullet
				bullet.Spread 	= Vector( 0, 0, 0 )			// Aim Cone
				bullet.Tracer	= 2									// Show a tracer on every x bullets 
				bullet.Force	= 5									// Amount of force to give to phys objects
				bullet.Damage	= dmg
				SWEPOWNER:LagCompensation( false )
				SWEPOWNER:FireBullets( bullet )
				SWEPOWNER:LagCompensation( true )
			end
		end
	self.Owner:FireBullets( bullet )
	self:TakePrimaryAmmo( 1 )
	self.Weapon:SendWeaponAnim( ACT_VM_PRIMARYATTACK )
	self.Owner:MuzzleFlash()
	self.Weapon:EmitSound( self.Primary.Sound )
	self.Owner:SetAnimation( PLAYER_ATTACK1 )								// Crappy muzzle light				// 3rd Person Animation
	if self.Owner:IsNPC() then return end
	
	if ( (SinglePlayer() && SERVER) || ( !SinglePlayer() && CLIENT && IsFirstTimePredicted() ) ) then
	
		local eyeang = self.Owner:EyeAngles()
		eyeang.pitch = eyeang.pitch - recoil
		self.Owner:SetEyeAngles( eyeang )
	end
end

function SWEP:DrawWeaponSelection( x, y, wide, tall, alpha )
	draw.SimpleText( self.IconLetter, "CSSelectIcons", x + wide/2, y + tall*0.2, Color( 255, 210, 0, 255 ), TEXT_ALIGN_CENTER )
end


function SWEP:SecondaryAttack()
	
end

function SWEP:DrawHUD()

	if self.Owner:ShouldDrawLocalPlayer() then return end

	local Crouch = ( self.Owner:Crouching() and self.Owner:OnGround() ) and 1 or 2

	local x, y = ScrW() / 2, ScrH() / 2
	local cone = self.Primary.Cone
	cone = ( self.Owner:GetVelocity():Length() > 150 ) and cone + ( self.Owner:GetVelocity():Length() / 25000 ) or cone
	local scale = 10 * cone

	local LastShootTime = self.Weapon:GetNetworkedFloat( "LastShootTime", 0 )
	scale = scale * (5 - math.Clamp( (CurTime() - LastShootTime) * 10, 0.0, 4.0 ))

	surface.SetDrawColor( 0, 255, 0, 255 )

	local gap = 50 * scale + 0.5 * Crouch
	local length = gap + 20 * scale + 2 * Crouch
	surface.DrawLine( x - length, y, x - gap, y )
	surface.DrawLine( x + length, y, x + gap, y )
	surface.DrawLine( x, y - length, x, y - gap )
	surface.DrawLine( x, y + length, x, y + gap )

	surface.DrawLine( x - 2, y, x + 2, y )
	surface.DrawLine( x, y - 2, x, y + 2 )
end

function SWEP:OnRestore()
	self.NextSecondaryAttack = 0
end
